<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将数据打包成二进制字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.ignore-user-abort.html">« ignore_user_abort</a></li>
      <li style="float: right;"><a href="function.php-strip-whitespace.html">php_strip_whitespace »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.misc.html">杂项 函数</a></li>
    <li>将数据打包成二进制字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.pack" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pack</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pack</span> &mdash; <span class="dc-title">将数据打包成二进制字符串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.pack-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pack</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   将输入参数打包成 <code class="parameter">format</code> 格式的二进制字符串。
  </p>
  <p class="para">
   这个函数的思想来自 Perl，所有格式化代码（<code class="parameter">format</code>）的工作原理都与
   Perl 相同。 但是，缺少了部分格式代码，比如 Perl 的 “u”。
  </p>
  <p class="para">
   注意，有符号值和无符号值之间的区别只影响函数
   <span class="function"><a href="function.unpack.html" class="function">unpack()</a></span>，在那些使用有符号和无符号格式代码的地方
   <span class="function"><strong>pack()</strong></span> 函数产生相同的结果。
  </p>
 </div>

 
 
 <div class="refsect1 parameters" id="refsect1-function.pack-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">format</code>
       字符串由格式代码组成，后面跟着一个可选的重复参数。重复参数可以是一个整数值或者
       <code class="literal">*</code> 值来重复到输入数据的末尾。对于 a, A, h, H
       格式化代码，其后的重复参数指定了给定数据将会被使用几个字符串。对于
       @，其后的数字表示放置剩余数据的绝对定位（之前的数据将会被空字符串填充），对于其他所有内容，重复数量指定消耗多少个数据参数并将其打包到生成的二进制字符串中。
      </p>



      <p class="para">
       目前已实现的格式如下：
       <table class="doctable table">
        <caption><strong><span class="function"><strong>pack()</strong></span> 格式字符</strong></caption>
        
         <thead>
          <tr>
           <th>代码</th>
           <th>描述</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>a</td>
           <td>以 NUL 字节填充字符串</td>
          </tr>

          <tr>
           <td>A</td>
           <td>以 SPACE(空格) 填充字符串</td></tr>

          <tr>
           <td>h</td>
           <td>十六进制字符串，低位在前</td></tr>

          <tr>
           <td>H</td>
           <td>十六进制字符串，高位在前</td></tr>

          <tr><td>c</td><td>有符号字符</td></tr>

          <tr>
           <td>C</td>
           <td>无符号字符</td></tr>

          <tr>
           <td>s</td>
           <td>有符号短整型(16位，主机字节序)</td>
          </tr>

          <tr>
           <td>S</td>
           <td>无符号短整型(16位，主机字节序)</td>
          </tr>

          <tr>
           <td>n</td>
           <td>无符号短整型(16位，大端字节序)</td>
          </tr>

          <tr>
           <td>v</td>
           <td>无符号短整型(16位，小端字节序)</td>
          </tr>

          <tr>
           <td>i</td>
           <td>有符号整型(机器相关大小字节序)</td>
          </tr>

          <tr>
           <td>I</td>
           <td>无符号整型(机器相关大小字节序)</td>
          </tr>

          <tr>
           <td>l</td>
           <td>有符号长整型(32位，主机字节序)</td>
          </tr>

          <tr>
           <td>L</td>
           <td>无符号长整型(32位，主机字节序)</td>
          </tr>

          <tr>
           <td>N</td>
           <td>无符号长整型(32位，大端字节序)</td>
          </tr>

          <tr>
           <td>V</td>
           <td>无符号长整型(32位，小端字节序)</td>
          </tr>

          <tr>
           <td>q</td>
           <td>有符号长长整型(64位，主机字节序)</td>
          </tr>

          <tr>
           <td>Q</td>
           <td>无符号长长整型(64位，主机字节序)</td>
          </tr>

          <tr>
           <td>J</td>
           <td>无符号长长整型(64位，大端字节序)</td>
          </tr>

          <tr>
           <td>P</td>
           <td>无符号长长整型(64位，小端字节序)</td>
          </tr>

          <tr>
           <td>f</td>
           <td>单精度浮点型(机器相关大小)</td>
          </tr>

          <tr>
           <td>g</td>
           <td>单精度浮点型(机器相关大小，小端字节序)</td>
          </tr>

          <tr>
           <td>G</td>
           <td>单精度浮点型(机器相关大小，大端字节序)</td>
          </tr>

          <tr>
           <td>d</td>
           <td>双精度浮点型(机器相关大小)</td>
          </tr>

          <tr>
           <td>e</td>
           <td>双精度浮点型(机器相关大小，小端字节序)</td>
          </tr>

          <tr>
           <td>E</td>
           <td>双精度浮点型(机器相关大小，大端字节序)</td>
          </tr>

          <tr>
           <td>x</td>
           <td>NUL 字节</td>
          </tr>

          <tr>
           <td>X</td>
           <td>回退一字节</td>
          </tr>

          <tr>
           <td>Z</td>
           <td>以 NUL 字节填充字符串空白</td>
          </tr>

          <tr>
           <td>@</td>
           <td>NUL 填充到绝对位置</td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">values</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.pack-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回包含数据的二进制字符串， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.pack-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        此函数不再在失败时返回 <strong><code>false</code></strong> 。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="type">float</span> 和 <span class="type">double</span> 类型支持大端和小端。
       </td>
      </tr>

      <tr>
       <td>7.0.15,7.1.1</td>
       <td>
        添加了 “e”,“E”,“g” 和 “G” 代码以启用 float 和 double 的字节顺序支持。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.pack-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3455">
    <p><strong>示例 #1 <span class="function"><strong>pack()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$binarydata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">"nvc*"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x1234</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x5678</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">65</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">66</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     输出结果为长度为 6 字节的二进制字符串，包含以下序列
     0x12, 0x34, 0x78, 0x56, 0x41, 0x42。
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.pack-notes">
  <h3 class="title">注释</h3>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">Note that PHP internally stores <span class="type">int</span> values as
    signed values of a machine-dependent size (C type <code class="literal">long</code>).
    Integer literals and operations that yield numbers outside the bounds of the
    <span class="type">int</span> type will be stored as <span class="type">float</span>.
    When packing these floats as integers, they are first cast into the integer
    type. This may or may not result in the desired byte pattern.
   </p>
   <p class="para">The most relevant case is when packing unsigned numbers that would
    be representable with the <span class="type">int</span> type if it were unsigned.
    In systems where the <span class="type">int</span> type has a 32-bit size, the cast
    usually results in the same byte pattern as if the <span class="type">int</span> were
    unsigned (although this relies on implementation-defined unsigned to signed
    conversions, as per the C standard). In systems where the
    <span class="type">int</span> type has 64-bit size, the <span class="type">float</span> most
    likely does not have a mantissa large enough to hold the value without
    loss of precision. If those systems also have a native 64-bit C
    <code class="literal">int</code> type (most UNIX-like systems don&#039;t), the only way to
    use the <code class="literal">I</code> pack format in the upper range is to create
    <span class="type">int</span> negative values with the same byte representation
    as the desired unsigned value.
   </p>
  </div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.pack-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.unpack.html" class="function" rel="rdfs-seeAlso">unpack()</a> - Unpack data from binary string</span></li>
   </ul>
  </p>
 </div>

 
</div></div></div></body></html>